package com.tgg.treeproject.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tgg.treeproject.pojo.SysDictType;
import com.tgg.treeproject.service.impl.SysDictTypeServiceImpl;
import com.tgg.treeproject.utils.NumberUtils;
import com.tgg.treeproject.utils.RedisUtil;
import com.tgg.treeproject.utils.SMSUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author 田国刚
 * @since 2023-04-16
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @PostMapping("/sendMessage")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> sendMessage(@RequestBody Map<String, Object> resout) throws UnsupportedEncodingException {
        String phone = resout.get("phone").toString();
        String number6 = NumberUtils.getNumber6();
        SMSUtil smsUtil = new SMSUtil();
        String s = smsUtil.sendMessage(phone, number6);
        JSONObject entries = JSONUtil.parseObj(s);
        Object resule = entries.get("resule");
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",entries);
        RedisUtil.setString(phone,number6,5*60);

        return new ResponseEntity<>(map, HttpStatus.OK);
    }
    @PostMapping("/loginuser")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, Object> resout) {
        String phone = resout.get("phone").toString();
        String code = resout.get("code").toString();
        HashMap<String, Object> map = new HashMap<>();
        String string = RedisUtil.getString(phone);

        if (code.equals(string)){
          map.put("data","");
          map.put("code",200);
          map.put("message","登陆成功");
        }else {
            map.put("data","");
            map.put("code",500);
            map.put("message","验证码错误或已失效，登陆失败");
        }
        return new ResponseEntity<>(map, HttpStatus.OK);
    }



}
